var express = require('express');
var router = express.Router();
var menuService = require("../service/menuService")
var typeService = require("../service/typeService")
var typeMenuService = require("../service/typeMenuService")
var typeRouterService = require("../service/typeRouterService")
var routerService = require("../service/routerService")



/* GET home page. */
router.get('/query', async function (req, res, next) {
    let { type } = req.query
    let params = { type }
    let result = await menuService.query({ type: 1 });

    // let result2 = await typeService.query({ type: { $in: params.type } })
    let result3 = await typeService.query({ type: { "$ne": 0 } })
    let result4 = await typeService.query({ type: { $in: [2, 3] } })
    let manageTeamMenu = await menuService.query({ usertype: { $in: [2, 3] } })

    //请求数据
    res.json({
        code: 200,
        msg: "菜单的查询接口",
        typeList: result3,
        result,
        manageTypeList: result4,
        manageTeamMenu
    })

});


//下拉框改变
router.get("/typemenuquery", async (req, res, next) => {
    let { typeid } = req.query;
    let result = await typeMenuService.query({ typeid });
    res.json({
        code: 200,
        data: result
    })

})

router.post("/add", async (req, res, next) => {
    let { name, path, type } = req.body;
    let result = await menuService.add({
        name, path, type
    })
    res.json({
        code: 200,
        msg: "新增成功",
        data: result
    })
})


router.post("/typemenuadd", async (req, res, next) => {
    let { typeid, menulist } = req.body;
    let list = menulist.map(item => {
        return {
            typeid,
            menuid: item._id,
            name: item.name,
            path: item.path
        }
    })

    //批量删除
    await typeMenuService.delMany({ typeid });
    let result = await typeMenuService.insertMany(list);
    res.json({
        code: 200,
        data: result,

    })
})



// 全部授权
router.post("/alltypemenuadd", async (req, res, next) => {
    let { typeid, menulist } = req.body;
    let menuList = []
    let routerList = []
    let list = menulist.map((item) => {
        menuList.push(item.path)
        return {
            typeid,
            menuid: item._id,
            name: item.name,
            path: item.path,
            menuList: item.path
        }
    })

    let res2 = await routerService.query({ path: { $in: menuList } })
    routerList = res2

    let routerInfo = routerList.map((item) => {
        return {
            typeid,
            routerid: item._id,
            name: item.name,
            path: item.path,
            com: item.com
        }
    })

    //批量删除
    await typeMenuService.delMany({ typeid });
    await typeRouterService.delMany({ typeid });

    let result = await typeMenuService.insertMany(list);
    let result2 = await typeRouterService.insertMany(routerInfo);
    res.json({
        code: 200,
        data: result,
        data1: result2

    })
})


module.exports = router;